package com.situ.mall.controller;

import com.situ.mall.pojo.Category;
import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.PageResult;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/list")
    public PageResult<Product> list(ProductQuery productQuery){
        PageResult<Product> pageResult = productService.list(productQuery);
        return pageResult;
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        productService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids){
        productService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/add")
    public Result add(Product product){
        productService.add(product);
        return Result.ok("添加成功");
    }

    @RequestMapping("/selectById")
    public Result selectById(Integer id){
        Product product = productService.selectById(id);
        List<Category> topCategoryList = categoryService.selectTopCategory();
        int topCategoryId = categoryService.selectTopCategoryByCategoryId(product.getCategoryId());
        List<Category> SecondCategoryList = categoryService.selectSecondCategoryByTopCategoryId(topCategoryId);
        Map<String,Object> map = new HashMap<>();
        map.put("product",product);
        map.put("topCategoryId",topCategoryId);
        map.put("topCategoryList",topCategoryList);
        map.put("SecondCategoryList",SecondCategoryList);
        return Result.ok(map);
    }

    @RequestMapping("/update")
    public Result update(Product product){
        productService.update(product);
        return Result.ok("更新成功");
    }
}
